<template>
  <Form class="xtx-form" :validation-schema="veeSchema" v-slot="{errors}" ref="formCom">
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <Field :class="{err:errors.accountApi}" v-model="form.accountApi" class="input" name='accountApi' type="text" placeholder="请输入用户名" />
      </div>
      <div class="error">{{errors.accountApi}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <Field :class="{err:errors.mobile}" v-model="form.mobile" class="input" name='mobile' type="text" placeholder="请输入手机号" />
      </div>
      <div class="error">{{errors.mobile}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <Field :class="{err:errors.code}" v-model="form.code" class="input" name='code' type="text" placeholder="请输入验证码" />
        <span class="code" @click="sendCode">{{time?`等待${time}秒`:'发送验证码'}}</span>
      </div>
      <div class="error">{{errors.code}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field :class="{err:errors.password}" v-model="form.password" class="input" name='password' type="password" placeholder="请输入密码" />
      </div>
      <div class="error">{{errors.password}}</div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <Field :class="{err:errors.rePassword}" v-model="form.rePassword" class="input" name='rePassword' type="password" placeholder="请确认密码" />
      </div>
      <div class="error">{{errors.rePassword}}</div>
    </div>
    <a href="javascript:;" class="submit" @click="login">立即提交</a>
  </Form>
</template>

<script>
import { useIntervalFn } from '@vueuse/core'
import veeSchema from '@/utils/vee-validate-schema'
import { Form, Field } from 'vee-validate'
import { inject, onUnmounted, reactive, ref } from 'vue'
import Message from '@/components/library/Message'
import { userQQPatchCode, userQQPatchLogin } from '@/api/user'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
export default {
  name: 'CallbackPatch',
  components: { Form, Field },
  setup () {
    // 定义表单数据
    const form = reactive({
      accountApi: null,
      mobile: null,
      code: null,
      password: null,
      rePassword: null
    })
    // 单独的抽出计时器
    const time = ref(0)
    const { pause, resume } = useIntervalFn(() => {
      time.value--
      if (time.value <= 0) {
        pause()
      }
    }, 1000, { immediate: false })
    // 组件卸载时清除定时器
    onUnmounted(() => {
      pause()
    })
    const formCom = ref(null)
    // 点击发送验证码
    const sendCode = async () => {
      if (time.value !== 0) return
      // 验证手机号
      const codeRes = veeSchema.mobile(form.mobile)
      if (codeRes === true) {
        try {
          await userQQPatchCode(form.mobile)
          time.value = 60
          resume()
        } catch (error) {
          Message({ type: 'error', text: error.response.data.message })
        }
      } else formCom.value.setFieldError('mobile', codeRes)
    }
    const store = useStore
    const router = useRouter
    // 点击立即提交
    const QQUserInfo = inject('QQUserInfo')
    const login = async () => {
      const valid = await formCom.value.validate()
      if (valid && QQUserInfo.value.openId) {
        try {
          const { result } = await userQQPatchLogin({ unionId: QQUserInfo.value.openId, mobile: form.mobile, code: form.code, account: form.accountApi, password: form.password })
          store.commit('user/setUser', result)
          router.push(store.state.user.redirectUrl)
          Message({ type: 'success', text: '登录成功' })
        } catch (error) {
          Message({ type: 'error', text: error.response.data.message })
        }
      }
    }
    return { form, veeSchema, time, sendCode, formCom, login }
  }
}
</script>

<style scoped lang='less'>
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
